package com.ailanyin.admin.runner;

import cn.hutool.core.util.RandomUtil;
import com.ailanyin.common.service.RedisService;
import com.ailanyin.security.utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2022/6/8 0008 下午 13:33
 */
@Component
@Order(value = 0)
@Slf4j
public class SysTokenSecretRunner implements ApplicationRunner {
    private static final String REDIS_SECRET_KEY = ":sys:secret:secret";

    @Autowired
    private RedisService redisService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (redisService.hasKey(REDIS_SECRET_KEY)) {
            TokenUtil.secret = redisService.get(REDIS_SECRET_KEY).toString();
        } else {
            TokenUtil.secret = RandomUtil.randomString(8);
            redisService.set(REDIS_SECRET_KEY, TokenUtil.secret);
        }
        log.info("生成Jwt密码成功");
    }
}
